# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver
from webdriver_base import WebdriverBase
from common.my_file import MyFile


'''
上交所监管数据
'''
'''
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")

file = open('./out/shangjiaosuo_jianguan_data.txt', 'w', encoding = 'utf-8')
'''

class ShJianguanData(WebdriverBase):
    def __init__(self):
        self.v3 = AttrV3()
        self.file = open('./txt/' + os.path.split(__file__)[-1].split(".")[0] + ".txt", 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def query_page_data(self, page):
        time.sleep(10)
        trs = self.driver.find_element_by_class_name("tdclickable").find_elements_by_class_name("isClickTr")
        for tr in trs:
            tds = tr.find_elements_by_tag_name("td")
            data_code = tds[0].text
            data_url = tds[3].find_element_by_tag_name("a").get_attribute("href")
            data_title = tds[3].find_element_by_tag_name("a").text
            data_date = tds[5].find_element_by_tag_name("div").get_attribute('innerText')

            table = self.v3.get_table_name(3002)
            try:
                self.v3.add_attr_data(table, data_code, data_title, data_date, data_url, "上交所")
            except Exception as e:
                self.write_log(str(e))
                pass
            self.file.write(data_code + "|0|3002|" + data_title + "|" + data_date + "|" + data_url + "|上交所|\n")
        
        next_page = self.driver.find_element_by_class_name("glyphicon-menu-right")
        if next_page:
            next_page.click()
            return True
        else:
            return False
        
    def query_data(self):
        self.driver.get("http://www.sse.com.cn/disclosure/credibility/supervision/measures/")
        page = 1
        while self.query_page_data(page):
            print(page)
            page = page + 1
            if page >= 5:
                break
            time.sleep(3)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()

def shangjiaosuo_jianguan_api():
    d = ShJianguanData()
    d.get_data()


if __name__ == '__main__':
    d = ShJianguanData()
    d.get_data()

